package main

import "fmt"

func createTestTable() error {
	query := fmt.Sprintf(`
    CREATE TABLE IF NOT EXISTS %s (
        id SERIAL PRIMARY KEY,
        
        -- 30个无索引字符字段
        no_index_char_1 VARCHAR(30),
        no_index_char_2 VARCHAR(30),
        no_index_char_3 VARCHAR(30),
        no_index_char_4 VARCHAR(30),
        no_index_char_5 VARCHAR(30),
        no_index_char_6 VARCHAR(30),
        no_index_char_7 VARCHAR(30),
        no_index_char_8 VARCHAR(30),
        no_index_char_9 VARCHAR(30),
        no_index_char_10 VARCHAR(30),
        no_index_char_11 VARCHAR(30),
        no_index_char_12 VARCHAR(30),
        no_index_char_13 VARCHAR(30),
        no_index_char_14 VARCHAR(30),
        no_index_char_15 VARCHAR(30),
        no_index_char_16 VARCHAR(30),
        no_index_char_17 VARCHAR(30),
        no_index_char_18 VARCHAR(30),
        no_index_char_19 VARCHAR(30),
        no_index_char_20 VARCHAR(30),
        no_index_char_21 VARCHAR(30),
        no_index_char_22 VARCHAR(30),
        no_index_char_23 VARCHAR(30),
        no_index_char_24 VARCHAR(30),
        no_index_char_25 VARCHAR(30),
        no_index_char_26 VARCHAR(30),
        no_index_char_27 VARCHAR(30),
        no_index_char_28 VARCHAR(30),
        no_index_char_29 VARCHAR(30),
        no_index_char_30 VARCHAR(30),
        
        -- 10个有索引字符字段
        index_char_1 VARCHAR(10),
        index_char_2 VARCHAR(10),
        index_char_3 VARCHAR(10),
        index_char_4 VARCHAR(10),
        index_char_5 VARCHAR(10),
        index_char_6 VARCHAR(10),
        index_char_7 VARCHAR(10),
        index_char_8 VARCHAR(10),
        index_char_9 VARCHAR(10),
        index_char_10 VARCHAR(10),
        
        -- 10个有索引数字字段
        index_num_1 INTEGER,
        index_num_2 INTEGER,
        index_num_3 INTEGER,
        index_num_4 INTEGER,
        index_num_5 INTEGER,
        index_num_6 INTEGER,
        index_num_7 INTEGER,
        index_num_8 INTEGER,
        index_num_9 INTEGER,
        index_num_10 INTEGER,
        
        -- 10个有索引时间字段
        index_time_1 TIMESTAMP,
        index_time_2 TIMESTAMP,
        index_time_3 TIMESTAMP,
        index_time_4 TIMESTAMP,
        index_time_5 TIMESTAMP,
        index_time_6 TIMESTAMP,
        index_time_7 TIMESTAMP,
        index_time_8 TIMESTAMP,
        index_time_9 TIMESTAMP,
        index_time_10 TIMESTAMP,
        
        -- 10个有索引数组字段
        index_array_1 TEXT[],
        index_array_2 TEXT[],
        index_array_3 TEXT[],
        index_array_4 TEXT[],
        index_array_5 TEXT[],
        index_array_6 TEXT[],
        index_array_7 TEXT[],
        index_array_8 TEXT[],
        index_array_9 TEXT[],
        index_array_10 TEXT[]
    )`, tableName)

	_, err := db.ExecContext(ctx, query)
	if err != nil {
		return fmt.Errorf("创建表失败: %v", err)
	}

	// 为所有有索引字段创建索引
	for i := 1; i <= 10; i++ {
		// 字符字段索引
		_, err = db.ExecContext(ctx, fmt.Sprintf(
			"CREATE INDEX IF NOT EXISTS idx_char_%d ON %s (index_char_%d)",
			i, tableName, i))
		if err != nil {
			return fmt.Errorf("创建字符字段索引失败: %v", err)
		}

		// 数字字段索引
		_, err = db.ExecContext(ctx, fmt.Sprintf(
			"CREATE INDEX IF NOT EXISTS idx_num_%d ON %s (index_num_%d)",
			i, tableName, i))
		if err != nil {
			return fmt.Errorf("创建数字字段索引失败: %v", err)
		}

		// 时间字段索引
		_, err = db.ExecContext(ctx, fmt.Sprintf(
			"CREATE INDEX IF NOT EXISTS idx_time_%d ON %s (index_time_%d)",
			i, tableName, i))
		if err != nil {
			return fmt.Errorf("创建时间字段索引失败: %v", err)
		}

		// 数组字段索引
		_, err = db.ExecContext(ctx, fmt.Sprintf(
			"CREATE INDEX IF NOT EXISTS idx_array_%d ON %s USING GIN (index_array_%d)",
			i, tableName, i))
		if err != nil {
			return fmt.Errorf("创建数组字段索引失败: %v", err)
		}
	}

	return nil
}